/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ConexaoDados;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author erikjan
 */
public class InteracaoDados {
    
    public InteracaoDados(){      
    }
    
    
    public void insereDados(String nome, String host, String no, boolean ativo, int tempo_a, int tempo) throws SQLException{
        
        Connection conexao = ConexaoMySQL.IniciaConexao();
        Statement stm = conexao.createStatement();
        String sql = "INSERT INTO servidor_opc(nome, host, no, ativo, tempo_a, tempoTT)   VALUES ('"+nome+"','"+host+"','"+no+"',"+ativo+","+tempo_a+","+tempo+")";
        //System.out.println("SQL :"+sql);  
        stm.executeUpdate(sql);
        stm.close();    
    }    
    
    public void inserir_unidade(String nomeunidade) throws SQLException{
      
        Connection conexao = ConexaoMySQL.IniciaConexao();
        Statement stm = conexao.createStatement();
        String sql;
        sql = "INSERT INTO unidade (nomeunidade) VALUES ('"+nomeunidade+"')";
        //System.out.println("SQL :"+sql);  
        stm.executeUpdate(sql);
        stm.close();       
    }
    
    public void inserir_planta(String nomeplanta, int id_unidade) throws SQLException{
      
        Connection conexao = ConexaoMySQL.IniciaConexao();
        Statement stm = conexao.createStatement();
        String sql;
        sql = "INSERT INTO planta (unidade_fk, nomeplanta) VALUES ("+id_unidade+",'"+nomeplanta+"')";
        //System.out.println("SQL :"+sql);  
        stm.executeUpdate(sql);
        stm.close();         
    }
   
    public void inserir_equipamento(String nomeequip, int id_equip) throws SQLException{

        Connection conexao = ConexaoMySQL.IniciaConexao();
        try (Statement stm = conexao.createStatement()){
            String sql;
            sql = "INSERT INTO equipamento (planta_fk, nomeequip) VALUES ("+id_equip+",'"+nomeequip+"')";
            //System.out.println("SQL :"+sql);  
            stm.executeUpdate(sql);
            stm.close();
        }   
    }
    
    public void inserir_malha(String nomemalha, int id_equip) throws SQLException{
        
        Connection conexao = ConexaoMySQL.IniciaConexao();
        try (Statement stm = conexao.createStatement()) {
            String sql;
            sql = "INSERT INTO malha (equipamento_fk, desc_malha) VALUES ("+id_equip+",'"+nomemalha+"')";
            //System.out.println("SQL :"+sql);  
            stm.executeUpdate(sql);
            stm.close();
        }   
    }   
   
    public void inserir_tag(String NomeTag, String TipoTag, String GrandezaTag, int tbmalha_fk, int servidoropc_fk) throws SQLException{
        
        Connection conexao = ConexaoMySQL.IniciaConexao();
        try (Statement stm = conexao.createStatement()) {
            String sql;                        
            sql = "INSERT INTO AipetroTest.associatag (tbmalha_fk, servidoropc_fk, NomeTag, TipoTag, GrandezaTag) VALUES ("+tbmalha_fk+","+servidoropc_fk+",'"+NomeTag+"','"+TipoTag+"','"+GrandezaTag+"')";
            System.out.println("SQL :"+sql);  
            stm.executeUpdate(sql);
            stm.close();
        } 
   }   
   
    
    public void inserir_historiador(String horaInicio, String horaColeta, String tipo, double valor, int tbmalha_fk) throws SQLException{
            
            Connection conexao = ConexaoMySQL.IniciaConexao();        
            try (Statement stm = conexao.createStatement()) {
                String sql;                        
                sql = "INSERT INTO AipetroTest.Historiador (horaInicio, horaColeta, tipo, valor, tbmalha_fk) VALUES ('"+horaInicio+"','"+horaColeta+"','"+tipo+"',"+valor+","+tbmalha_fk+")";                
                stm.executeUpdate(sql);
                stm.close();
                if (conexao.isClosed()==false)//se estiver conectado
                    {
                    conexao.close();
                    }            
        } 
           
       }    
    
    public void inserir_avaliacao(String data_atual, double harris, double goodhart, double ITSE, double ITAE, double nota, int id_malha) throws SQLException{
            
            Connection conexao = ConexaoMySQL.IniciaConexao();        
            try (Statement stm = conexao.createStatement()) 
            {
                String sql;                        
                sql = "INSERT INTO indice_avaliacao (data_atual, harris, goodhart, ITSE, ITAE, Nota, malha_fk) VALUES ('"+data_atual+"',"+harris+","+goodhart+","+ITSE+","+ITAE+","+nota+","+id_malha+")";                
                stm.executeUpdate(sql);
                stm.close();
                if (conexao.isClosed()==false)//se estiver conectado
                    {
                    conexao.close();
                    }            
            } 
           
       }        
    
    public ResultSet consulta(String sql) throws SQLException{
      
        ResultSet retorno;
        Connection conexao = ConexaoMySQL.IniciaConexao();
        Statement stm = conexao.createStatement();
        retorno = stm.executeQuery(sql);
        //stm.close();                       
        return retorno;       
      }
    
    public int consulta_item(String sql) throws SQLException{
     
        Connection conexao = ConexaoMySQL.IniciaConexao();
        Statement stm = conexao.createStatement();
        //System.out.println("SQL :"+sql); 
        ResultSet rs = stm.executeQuery(sql);
        rs.next();
        return rs.getInt(1);       
      }
    
    public void deleta(String sql) throws SQLException{
      
        Connection conexao = ConexaoMySQL.IniciaConexao();
        Statement stm = conexao.createStatement();
        stm.executeUpdate(sql);      
        //System.out.println("SQL :"+sql); 
        stm.close();         
      }          
}
